package org.projects.graduates.domain;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

@Entity
@Table(name = "person_users")
public class PersonUser extends AbstractEntity {
	private static final long serialVersionUID = 9079738806864110756L;

	@OneToOne
	private Person person;

	@OneToOne
	private User user;

	public PersonUser() {
		super();
	}

	public PersonUser(Person person, User user) {
		this.person = person;
		this.user = user;
	}

	public static Person getPersonBy(User user) {
		QuerySettings<PersonUser> settings = QuerySettings.create(PersonUser.class);
		settings.eq("user", user);
		PersonUser pu = getRepository().getSingleResult(settings);
		if (pu != null) {
			return pu.getPerson();
		}
		return null;
	}

	public static User getUserBy(Person person) {
		QuerySettings<PersonUser> settings = QuerySettings.create(PersonUser.class);
		settings.eq("person", person);
		PersonUser pu = getRepository().getSingleResult(settings);
		if (pu != null) {
			return pu.getUser();
		}
		return null;
	}

	@Override
	public int hashCode() {
		return 0;
	}

	/**
	 * 要改!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	 */
	public boolean equals(Object other) {
		if (other != null && other instanceof PersonUser) {
			return true;
		}

		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
